package com.inkyi.assistant.app.component.rsyntax;


import com.inkyi.assistant.app.component.rsyntax.action.*;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rtextarea.RTextScrollPane;

import javax.swing.*;
import java.awt.*;
import java.util.List;


public class RSyntaxTextToolBar extends JToolBar  {

    private final RSyntaxTextArea textArea;
    private final RTextScrollPane scrollPane;

    // 主题
    private JComboBox<String> themeComboBox;
    private DefaultComboBoxModel<String> themeListModel;
    // 语言
    private JComboBox<String> styleComboBox;
    private DefaultComboBoxModel<String> styleListModel;
    // 字体名字
    private JComboBox<String> fontNameComboBox;
    private DefaultComboBoxModel<String> fontNameListModel;
    // 字体大小
    private JComboBox<String> fontSizeComboBox;
    private DefaultComboBoxModel<String> fontSizeListModel;
    // 当前行高亮
    private JCheckBox viewLineHighlightCheckBox;
    // 显示行号
    private JCheckBox viewLineNumberCheckBox;
    // 自动换行
    private JCheckBox wordWrapCheckBox;
    // 代码折叠
    private JCheckBox codeFoldingCheckBox;

    public RSyntaxTextToolBar(RSyntaxTextArea textArea, RTextScrollPane scrollPane) {
        this.textArea = textArea;
        this.scrollPane = scrollPane;
        setupUI();
        initConfig();
    }

    public void setupUI() {
        // 主题
        themeComboBox = new JComboBox<>();
        themeComboBox.setToolTipText("切换主题");
        themeListModel = new DefaultComboBoxModel<>();
        themeComboBox.setModel(themeListModel);
        this.add(themeComboBox);
        this.addSeparator();
        // 文本格式
        styleComboBox = new JComboBox<>();
        styleComboBox.setToolTipText("文本格式");
        styleListModel = new DefaultComboBoxModel<>();
        styleComboBox.setModel(styleListModel);
        this.add(styleComboBox);
        this.addSeparator();
        // 字体名字
        fontNameComboBox = new JComboBox<>();
        fontNameComboBox.setToolTipText("字体");
        fontNameListModel = new DefaultComboBoxModel<>();
        fontNameComboBox.setModel(fontNameListModel);
        this.add(fontNameComboBox);
        // 字体大小
        fontSizeComboBox = new JComboBox<>();
        fontSizeComboBox.setToolTipText("字号");
        fontSizeListModel = new DefaultComboBoxModel<>();
        fontSizeComboBox.setModel(fontSizeListModel);
        this.add(fontSizeComboBox);
        this.addSeparator();
        // 当前行高亮
        viewLineHighlightCheckBox = new JCheckBox("当前行高亮");
        this.add(viewLineHighlightCheckBox);
        // 显示行号
        viewLineNumberCheckBox = new JCheckBox("显示行号");
        this.add(viewLineNumberCheckBox);
        // 自动换行
        wordWrapCheckBox = new JCheckBox("自动换行");
        this.add(wordWrapCheckBox);
        // 代码折叠
        codeFoldingCheckBox = new JCheckBox("代码折叠");
        this.add(codeFoldingCheckBox);

    }

    public void initConfig() {
        // 禁用浮动功能
        this.setFloatable(false);
        // 启用悬停效果
        this.setRollover(true);
        //
        List<String> themeKeys = ChangeThemeAction.getThemeKey();
        for (String themeKey : themeKeys) {
            themeListModel.addElement(themeKey);
        }
        themeComboBox.addActionListener(new ChangeThemeAction(textArea));
        themeComboBox.setSelectedItem("Default (System Selection)");
        //
        List<String> styleKeys = ChangeSyntaxStyleAction.getStyleKey();
        for (String key : styleKeys) {
            styleListModel.addElement(key);
        }
        styleComboBox.addActionListener(new ChangeSyntaxStyleAction(textArea));
        styleComboBox.setSelectedItem("NONE");
        // 字体,获取系统字体
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fonts = ge.getAvailableFontFamilyNames();
        for (String font : fonts) {
            fontNameComboBox.addItem(font);
        }
        // 字体大小
        for (int i = 5; i <= 30; i++) {
            fontSizeComboBox.addItem(String.valueOf(i));
        }
        // 监听事件
        viewLineHighlightCheckBox.addItemListener(new ViewLineHighlightAction(textArea));
        viewLineNumberCheckBox.addItemListener(new ViewLineNumbersAction(scrollPane));
        wordWrapCheckBox.addItemListener(new WordWrapAction(textArea));
        codeFoldingCheckBox.addActionListener(new CodeFoldingAction(textArea));
        fontNameComboBox.addActionListener(new FontNameAction(textArea));
        fontSizeComboBox.addActionListener(new FontSizeAction(textArea));
        // 初始化 ，字号20
        fontSizeComboBox.setSelectedItem(String.valueOf(20));
        String fontName = textArea.getFont().getFontName();
        fontNameComboBox.addItem(fontName);
        fontNameComboBox.setSelectedItem(fontName);
    }

    public JComboBox<String> getStyleComboBox() {
        return styleComboBox;
    }

    public JComboBox<String> getThemeComboBox() {
        return themeComboBox;
    }

    public JComboBox<String> getFontNameComboBox() {
        return fontNameComboBox;
    }

    public JComboBox<String> getFontSizeComboBox() {
        return fontSizeComboBox;
    }

    public JCheckBox getViewLineHighlightCheckBox() {
        return viewLineHighlightCheckBox;
    }

    public JCheckBox getViewLineNumberCheckBox() {
        return viewLineNumberCheckBox;
    }

    public JCheckBox getWordWrapCheckBox() {
        return wordWrapCheckBox;
    }

    public JCheckBox getCodeFoldingCheckBox() {
        return codeFoldingCheckBox;
    }
}
